home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wics.zip / DEDTWIN.CPP < prev    next >
C/C++ Source or Header  |  1993-03-05  |  11KB  |  462 lines

  1. #include <dedtwin.h>
  2.  
  3. TDemoEditWindow::TDemoEditWindow (PTWindowsObject AParent, LPSTR ATitle, LPSTR AFile)
  4.     : TFileWindow (AParent, ATitle, AFile)
  5. {
  6.     lf.lfHeight = -13 ;
  7.     lf.lfWidth  = 0;
  8.     lf.lfEscapement = 0;
  9.     lf.lfOrientation = 0;
  10.     lf.lfWeight = FW_REGULAR;
  11.     lf.lfItalic = FALSE;
  12.     lf.lfUnderline = FALSE;
  13.     lf.lfStrikeOut = FALSE;
  14.     lf.lfCharSet = ANSI_CHARSET;
  15.     lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  16.     lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  17.     lf.lfQuality = DEFAULT_QUALITY;
  18.     lf.lfPitchAndFamily = DEFAULT_PITCH || FF_SWISS;
  19.     lstrcpy ( lf.lfFaceName, "Arial");
  20.  
  21.     hFont = CreateFontIndirect (&lf);
  22.  
  23.     fr.lStructSize = sizeof(FINDREPLACE);
  24.     fr.hInstance   = GetApplication()->hInstance;
  25.     fr.Flags       = FR_DOWN | FR_MATCHCASE | FR_SHOWHELP;
  26.     fr.lpstrFindWhat = new char[128];
  27.     fr.lpstrReplaceWith = new char [128];
  28.     fr.wFindWhatLen = 128;
  29.     fr.wReplaceWithLen = 128;
  30.  
  31.     lstrcpy (fr.lpstrFindWhat, "");
  32.     lstrcpy (fr.lpstrReplaceWith, "");
  33. }
  34.  
  35. TDemoEditWindow::~TDemoEditWindow()
  36. {
  37.     DeleteObject (hFont);
  38. }
  39.  
  40. void TDemoEditWindow::GetWindowClass(WNDCLASS _FAR & AWndClass)
  41. {
  42.     TFileWindow::GetWindowClass(AWndClass);
  43.     AWndClass.hIcon = LoadIcon(GetApplication()->hInstance, MAKEINTRESOURCE(FILE_ICON));
  44. }
  45.  
  46. void TDemoEditWindow::SetupWindow ()
  47. {
  48.     TFileWindow::SetupWindow();
  49.  
  50.     SendMessage (Editor->HWindow, WM_SETFONT, hFont, 0L);
  51. }
  52.  
  53. void TDemoEditWindow::DefWndProc (RTMessage Msg)
  54. {
  55.     if ( Msg.Message == GetParent()->idFindTextMessage )
  56.     {
  57.         LPFINDREPLACE    lpfr ;
  58.  
  59.         lpfr = (LPFINDREPLACE) Msg.LParam ;
  60.  
  61.         if ( lpfr->Flags & FR_REPLACE )
  62.             ReplaceEditorText ();
  63.         else
  64.             if ( lpfr->Flags & FR_REPLACEALL )
  65.                 ReplaceAllEditorText ();
  66.             else
  67.                 if ( !(lpfr->Flags & FR_DIALOGTERM) )
  68.                     FindEditorText ();
  69.                 else
  70.                     TFileWindow::DefWndProc(Msg);
  71.     }
  72.     else
  73.         TFileWindow::DefWndProc(Msg);
  74. }
  75.  
  76. void TDemoEditWindow::ReplaceEditorText ()
  77. {
  78.     if ( Editor->Search(-1, fr.lpstrFindWhat, fr.Flags & FR_MATCHCASE) != -1 )
  79.         Editor->Insert ( fr.lpstrReplaceWith );
  80. }
  81.  
  82. void TDemoEditWindow::ReplaceAllEditorText ()
  83. {
  84.     while ( Editor->Search (-1, fr.lpstrFindWhat, fr.Flags & FR_MATCHCASE) != -1 )
  85.         Editor->Insert ( fr.lpstrReplaceWith );
  86. }
  87.  
  88. void TDemoEditWindow::WMCommand (RTMessage msg)
  89. {
  90.     if ( HIWORD(msg.LParam) == EN_SETFOCUS )
  91.         ENSetFocus();
  92.  
  93.     if ( HIWORD(msg.LParam) == EN_KILLFOCUS )
  94.         ENKillFocus();
  95.  
  96.     TFileWindow::WMCommand(msg);
  97. }
  98.  
  99. void TDemoEditWindow::CMFind (RTMessage)
  100. {
  101.     int    StartPos, EndPos ;
  102.  
  103.     fr.hwndOwner   = HWindow;
  104.     fr.Flags = FR_DOWN | FR_MATCHCASE | FR_SHOWHELP;
  105.  
  106.     Editor->GetSelection(StartPos, EndPos);
  107.  
  108.     if ( StartPos != EndPos )
  109.         Editor->GetSubText (fr.lpstrFindWhat, StartPos, EndPos);
  110.     else
  111.         lstrcpy (fr.lpstrFindWhat, "");
  112.  
  113.     FindText (&fr);
  114. }
  115.  
  116. void TDemoEditWindow::CMReplace (RTMessage)
  117. {
  118.     int    StartPos, EndPos ;
  119.  
  120.     fr.hwndOwner   = HWindow;
  121.     fr.Flags = FR_DOWN | FR_MATCHCASE | FR_SHOWHELP;
  122.  
  123.     Editor->GetSelection(StartPos, EndPos);
  124.  
  125.     if ( StartPos != EndPos )
  126.         Editor->GetSubText (fr.lpstrFindWhat, StartPos, EndPos);
  127.     else
  128.         lstrcpy (fr.lpstrFindWhat, "");
  129.  
  130.     ReplaceText (&fr);
  131. }
  132.  
  133. void TDemoEditWindow::CMPrint (RTMessage)
  134. {
  135.     PTDemoPrintedPage    DemoPrintout = 0;
  136.  
  137.     if ( GetPrinter() )
  138.     {
  139.         DemoPrintout = new TDemoPrintedPage ("WICS Demo", Editor, &lf);
  140.  
  141.         GetPrinter()->Print (GetParent(), DemoPrintout);
  142.  
  143.         delete DemoPrintout ;
  144.     }
  145. }
  146.  
  147. TDemoPrintedPage::TDemoPrintedPage (Pchar ATitle, PTEdit pe, LOGFONT FAR * lplf)
  148.         : TPrintedPage(ATitle)
  149. {
  150.     AnEditor  = pe;
  151.     lpLogFont = lplf;
  152.  
  153.     iLine = 0 ;
  154.     iOffsetToLine = 0;
  155.     fAnotherPage = TRUE;
  156. }
  157.  
  158. void TDemoPrintedPage::PrintPage (HDC hdc, WORD, POINT PageSize, POINT dpi)
  159. {
  160.     HFONT    hFont;
  161.     LPSTR    lpLine;
  162.     DWORD    lExtent;
  163.     LOGFONT    lf;
  164.     int        iPrevLine;
  165.     int        Pagecx;
  166.     int        Pagecy;
  167.     int        PointSize;
  168.     int        i, n;
  169.     int        length;
  170.     int        x, y ;
  171.  
  172.     const UnitsPerInch = 100 ;
  173.  
  174.     Pagecx = (int)((long)PageSize.x * UnitsPerInch / dpi.x) ;
  175.     Pagecy = (int)((long)PageSize.y * UnitsPerInch / dpi.y) ;
  176.  
  177.     PointSize = GetPointSize (lpLogFont);            //    Get the point size of the font
  178.     lpLine = new char [256];                        //    Allocate a buffer for the input line
  179.     _fmemmove (&lf, lpLogFont, sizeof(LOGFONT));    //    Copy the LOGFONT structure
  180.     iPrevLine = -1;
  181.  
  182.     SaveDC (hdc);                                    //    Save the Device Context on the stack
  183.     SetMapMode (hdc, MM_LOENGLISH);                    //    Set the mapping mode
  184.                         
  185.     lf.lfHeight = 0 - (int)((long) PointSize * UnitsPerInch / 72) ;
  186.  
  187.     hFont = CreateFontIndirect (&lf);                //    Create the printer font
  188.     SelectObject (hdc, hFont);                        //    Select the printer font
  189.     SetTextColor (hdc, RGB(0,0,0));                    //    Set Text Color to Black
  190.  
  191.     lExtent = GetTextExtent (hdc, "A", 1);            //    Get the line height
  192.  
  193.     x = (0.50 * UnitsPerInch);                        //    1/2 inch border
  194.     y =  0 - x ;                                            //    1/2 inch border
  195.  
  196.     for ( i = iLine ; i < AnEditor->GetNumLines() && 0 - y < Pagecy ; )
  197.     {
  198.         length = AnEditor->GetLineLength(i);
  199.  
  200.         if ( length > 0 )
  201.         {
  202.             if ( length > 256 )
  203.                 length = 255 ;
  204.  
  205.             if ( i != iPrevLine )
  206.             {
  207.                 AnEditor->GetLine(lpLine, length, i);
  208.                 iPrevLine = i ;
  209.             }
  210.  
  211.             lExtent = GetTextExtent (hdc, (lpLine+iOffsetToLine), lstrlen(lpLine+iOffsetToLine));
  212.  
  213.             if ( LOWORD(lExtent) < Pagecx )
  214.             {
  215.                 TextOut (hdc, x, y, (lpLine+iOffsetToLine), lstrlen(lpLine+iOffsetToLine));
  216.                 iOffsetToLine = 0;
  217.                 iLine++;
  218.                 i++;
  219.             }
  220.             else
  221.             {
  222.                 n = lstrlen(lpLine) - 1 ;
  223.                 lExtent = GetTextExtent (hdc, (lpLine+iOffsetToLine), n - iOffsetToLine) ;
  224.  
  225.                 while ( LOWORD(lExtent) > Pagecx && n > iOffsetToLine )
  226.                 {
  227.                     while ( *(lpLine+n) == ' ' && n > iOffsetToLine )
  228.                         n--;
  229.  
  230.                     while ( *(lpLine+n) != ' ' && n > iOffsetToLine )
  231.                         n-- ;
  232.  
  233.                     if ( n > iOffsetToLine )
  234.                         lExtent = GetTextExtent (hdc, (lpLine+iOffsetToLine), n - iOffsetToLine) ;
  235.                 }
  236.  
  237.                 if ( n > iOffsetToLine )
  238.                 {
  239.                     TextOut (hdc, x, y, (lpLine+iOffsetToLine), n - iOffsetToLine);
  240.                     iOffsetToLine = n ;
  241.  
  242.                     while ( *(lpLine+iOffsetToLine) != 0x00 && *(lpLine+iOffsetToLine) == ' ' )
  243.                         iOffsetToLine++;
  244.  
  245.                     if ( *(lpLine+iOffsetToLine) == 0x00 )
  246.                     {
  247.                         iOffsetToLine = 0;
  248.                         iLine++;
  249.                         i++;
  250.                     }
  251.                 }
  252.                 else
  253.                 {
  254.                     TextOut (hdc, x, y, (lpLine+iOffsetToLine), lstrlen(lpLine+iOffsetToLine));
  255.                     iOffsetToLine = 0;
  256.                     iLine++;
  257.                     i++;
  258.                 }
  259.             }
  260.         }
  261.         else
  262.         {
  263.             iPrevLine = i;
  264.             iOffsetToLine = 0;
  265.             iLine++;
  266.             i++;
  267.         }
  268.  
  269.         y -= HIWORD(lExtent);
  270.     }
  271.  
  272.     if ( i < AnEditor->GetNumLines() )
  273.         fAnotherPage = TRUE ;
  274.     else
  275.         fAnotherPage = FALSE ;
  276.  
  277.     RestoreDC (hdc, -1);
  278.  
  279.     DeleteObject (hFont);
  280.     delete lpLine;
  281. }
  282.  
  283. void TDemoEditWindow::ENSetFocus ()
  284. {
  285.     BOOL    fBold;
  286.     BOOL    fItalic;       
  287.  
  288.     fBold = ( lf.lfWeight > FW_REGULAR ) ;
  289.     fItalic = (BOOL)lf.lfItalic;
  290.  
  291.     GetParent()->SetIconBarButtonState (IBID_BOLD, fBold);
  292.     GetParent()->SetIconBarButtonState (IBID_ITALIC, fItalic);
  293.     GetParent()->SetFontStyle(&lf);
  294.  
  295.     EnableMenuEntry (CM_FILESAVE);
  296.     EnableMenuEntry (CM_FILESAVEAS);
  297.     EnableMenuEntry (CM_PRINT);
  298.     EnableMenuEntry (CM_EDITUNDO);
  299.     EnableMenuEntry (CM_EDITCUT);
  300.     EnableMenuEntry (CM_EDITCOPY);
  301.     EnableMenuEntry (CM_EDITPASTE);
  302.     EnableMenuEntry (CM_EDITCLEAR);
  303.     EnableMenuEntry (CM_CHANGEFONT);
  304.     EnableMenuEntry (CM_SELECTCOLOR);
  305.     EnableMenuEntry (CM_FIND);
  306.     EnableMenuEntry (CM_REPLACE);
  307. }
  308.  
  309. void TDemoEditWindow::ENKillFocus ()
  310. {
  311.     DisableMenuEntry (CM_FILESAVE);
  312.     DisableMenuEntry (CM_FILESAVEAS);
  313.     DisableMenuEntry (CM_PRINT);
  314.     DisableMenuEntry (CM_EDITUNDO);
  315.     DisableMenuEntry (CM_EDITCUT);
  316.     DisableMenuEntry (CM_EDITCOPY);
  317.     DisableMenuEntry (CM_EDITPASTE);
  318.     DisableMenuEntry (CM_EDITCLEAR);
  319.     DisableMenuEntry (CM_CHANGEFONT);
  320.     DisableMenuEntry (CM_SELECTCOLOR);
  321.     DisableMenuEntry (CM_FIND);
  322.     DisableMenuEntry (CM_REPLACE);
  323. }
  324.  
  325. void TDemoEditWindow::CMFontChanged (RTMessage)
  326. {
  327.     HFONT    hOldFont ;
  328.  
  329.     GetParent()->GetIconBar()->GetSelectedFontFamilyName(LF_FACESIZE-1,lf.lfFaceName);
  330.     lf.lfCharSet = DEFAULT_CHARSET;
  331.     lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  332.     lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  333.     lf.lfQuality = DEFAULT_QUALITY;
  334.     lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE ;
  335.  
  336.     hOldFont = hFont;
  337.     hFont = CreateFontIndirect(&lf);
  338.     SendMessage (Editor->HWindow, WM_SETFONT, hFont, MAKELPARAM(TRUE,0));
  339.     DeleteObject (hOldFont);
  340. }
  341.  
  342. void TDemoEditWindow::CMFontSizeChanged (RTMessage)
  343. {
  344.     HFONT    hOldFont;
  345.  
  346.     lf.lfHeight = GetParent()->GetIconBar()->GetSelectedFontSize();
  347.  
  348.     hOldFont = hFont;
  349.     hFont = CreateFontIndirect(&lf);
  350.     SendMessage (Editor->HWindow, WM_SETFONT, hFont, MAKELPARAM(TRUE,0));
  351.     DeleteObject (hOldFont);
  352. }
  353.  
  354. void TDemoEditWindow::CMBold (RTMessage)
  355. {
  356.     BOOL    fBold ;
  357.     HFONT    hOldFont;
  358.  
  359.     if ( lf.lfWeight > FW_REGULAR )
  360.     {
  361.         lf.lfWeight -= 300 ;
  362.         fBold = FALSE;
  363.     }
  364.     else
  365.     {
  366.         lf.lfWeight += 300 ;
  367.         fBold = TRUE ;
  368.     }
  369.  
  370.     GetParent()->GetIconBar()->SetButtonState (IBID_BOLD, fBold);
  371.  
  372.     hOldFont = hFont;        
  373.     hFont = CreateFontIndirect(&lf);
  374.     SendMessage (Editor->HWindow, WM_SETFONT, hFont, MAKELPARAM(TRUE,0));
  375.     DeleteObject (hOldFont);
  376. }
  377.  
  378. void TDemoEditWindow::CMItalic (RTMessage)
  379. {
  380.     BOOL    fItalic ;
  381.     HFONT    hOldFont;
  382.  
  383.     fItalic = !(BOOL) lf.lfItalic ;
  384.  
  385.     lf.lfItalic = fItalic ;
  386.     GetParent()->GetIconBar()->SetButtonState (IBID_ITALIC, fItalic);
  387.  
  388.     hOldFont = hFont;
  389.     hFont = CreateFontIndirect(&lf);
  390.     SendMessage (Editor->HWindow, WM_SETFONT, hFont, MAKELPARAM(TRUE,0));
  391.     DeleteObject (hOldFont);
  392. }
  393.  
  394. void TDemoEditWindow::CMChangeFont (RTMessage)
  395. {
  396.     CHOOSEFONT    cf;
  397.  
  398.     cf.lStructSize    = sizeof(CHOOSEFONT);
  399.     cf.hwndOwner      = HWindow;
  400.     cf.hDC            = NULL;
  401.     cf.lpLogFont      = &lf;
  402.     cf.Flags      = CF_ANSIONLY | CF_TTONLY | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_SHOWHELP ;
  403.     cf.rgbColors      = rgbTextColor;
  404.     cf.lCustData      = NULL;
  405.     cf.lpfnHook       = NULL;
  406.     cf.lpTemplateName = NULL;
  407.     cf.hInstance      = NULL;
  408.     cf.lpszStyle      = NULL;
  409.     cf.nFontType      = 0;
  410.     cf.nSizeMin       = 0;
  411.     cf.nSizeMax       = 0;
  412.  
  413.     if ( ChooseFont (&cf) )
  414.     {
  415.         DeleteObject (hFont);
  416.         hFont = CreateFontIndirect(&lf);
  417.         SendMessage (Editor->HWindow, WM_SETFONT, hFont, MAKELPARAM(TRUE,0));
  418.     }
  419. }
  420.  
  421. void TDemoEditWindow::CMFileSaveAs (RTMessage)
  422. {
  423.     OPENFILENAME    ofn ;
  424.     LPSTR             szDefaultDirectory;
  425.  
  426.     szDefaultDirectory = new char[128];
  427.  
  428.     ofn.lStructSize         = sizeof(OPENFILENAME);
  429.     ofn.hwndOwner           = HWindow;
  430.     ofn.hInstance           = GetApplication()->hInstance;
  431.     ofn.lpstrFilter         = "All Files\x00*.*\x00\x00\x00";
  432.     ofn.lpstrCustomFilter         = NULL;
  433.     ofn.nMaxCustFilter        = 0;
  434.     ofn.nFilterIndex        = 1;
  435.     ofn.lpstrFile            = new char[128];
  436.     ofn.nMaxFile            = 128;
  437.     ofn.lpstrFileTitle        = NULL;
  438.     ofn.nMaxFileTitle        = 0;
  439.     ofn.lpstrInitialDir        = szDefaultDirectory;
  440.     ofn.lpstrTitle            = "Save File";
  441.     ofn.Flags            = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_SHOWHELP | OFN_NOREADONLYRETURN ; 
  442.     ofn.nFileOffset            = 0;
  443.     ofn.nFileExtension        = 0;
  444.     ofn.lpstrDefExt            = "DAT";
  445.     ofn.lCustData            = NULL;
  446.     ofn.lpfnHook            = NULL;
  447.     ofn.lpTemplateName        = NULL;
  448.  
  449.     lstrcpy (ofn.lpstrFile, FileName);
  450.     lstrcpy (szDefaultDirectory, "C:\\");
  451.  
  452.     if ( GetSaveFileName (&ofn) )
  453.     {
  454.         SetFileName(ofn.lpstrFile);
  455.         Write();
  456.     }
  457.  
  458.     delete szDefaultDirectory;
  459.     delete ofn.lpstrFile;
  460. }
  461.  
  462.